package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.system.EntitySysProject;
import com.loongtech.bi.entity.system.EntitySysUserRole;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.RetCode;
import com.loongtech.core.util.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysProjectManager extends ManagerQueryCacheBase<EntitySysProject> {
    private static final long serialVersionUID = 1L;
    @Resource
    SysUserProjectRoleManager sysUserProjectRoleManager;

    public static Map<String, String> projectIdNameMap;

    public void init() {
        projectIdNameMap = new HashMap<>();
        List<EntitySysProject> res = findAll();
        if (res != null && res.size() > 0) {
            for (EntitySysProject re : res) {
                projectIdNameMap.put(re.getId().toString(), re.getProjectName());
            }
        }
    }

    /**
     * 添加项目
     */
    public RetCode addProject(EntitySysProject project) {
        try {

            // 判断用户name是否已经被注册
            if (null != getBy("projectName", project.getProjectName())) {
                return RetCode.eNameExist;
            }
            if (null != getBy("id", project.getId())) {
                return RetCode.eIdExist;
            }
            persist(project);
            init();
            return RetCode.eSuccess;
        } catch (Exception ex) {
            ex.printStackTrace();
            return RetCode.eOtherException;
        }
    }

    /**
     * 删除项目
     */
    public RetCode delProject(Integer id) {
        try {
            List<EntitySysUserRole> list = sysUserProjectRoleManager.findAll(EntitySysUserRole.K.roleId, id);
            if (null != list && !list.isEmpty()) {
                return RetCode.eRelationExist;
            }
            if (null != get(id)) {
                removeById(id);
                init();
                return RetCode.eSuccess;
            }
            return RetCode.eModuleEmpty;
        } catch (Exception ex) {
            ex.printStackTrace();
            return RetCode.eOtherException;
        }
    }

    /**
     * 修改项目
     */
    public RetCode modifyProject(EntitySysProject e) {
        try {
            EntitySysProject cur = get(e.getId());
            if (cur != null) {
                e.setCreatTime(cur.getCreatTime());
                merge(e);
                init();
                return RetCode.eSuccess;
            } else {
                return RetCode.eModuleEmpty;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return RetCode.eOtherException;
        }
    }

    /**
     * 获取项目列表
     */
    public List<EntitySysProject> queryList() {
        try {
            return findByQuery("from EntitySysProject order by sortId asc");
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 查询项目
     */
    public List<EntitySysProject> searchByCriteria(EntitySysProject e) {
        List<Object> list = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntitySysProject e where 1=1 ");
        if (!Utils.isEmptyStr(e.getProjectName())) {
            hql.append(" and e.projectName like ?");
            list.add("%" + e.getProjectName() + "%");
        }
        if (!Utils.isEmptyStr(e.getProjectUrl())) {
            hql.append(" and e.projectUrl like ?");
            list.add("%" + e.getProjectUrl() + "%");
        }
        hql.append(" order by e.creatTime desc");
        return findByQuery(hql.toString(), list.toArray());
    }
}
